package cn.doitedu.dw.utils;

import java.util.ArrayList;
import java.util.List;

public class BinarySearcher {

    public static <T extends  Comparable> int search(List<T> lst,T value){
        int start = 0;
        int end = lst.size()-1;
        int mid;

        while(start <= end){
            mid = (end-start)/2 + start;
            if(lst.get(mid).compareTo(value)>0){
                end = mid-1;
            }else if(lst.get(mid).compareTo(value)<0){
                start = mid+1;
            }else{
                return mid;
            }
        }
        return -1;
    }


    public static void main(String[] args) {
        ArrayList<IpInfoBean> lst = new ArrayList<>();
        IpInfoBean ip1 = new IpInfoBean(1, 5, "c1", "p1");
        IpInfoBean ip2 = new IpInfoBean(7, 15, "c2", "p2");
        IpInfoBean ip3 = new IpInfoBean(20, 25, "c3", "p3");
        IpInfoBean ip4 = new IpInfoBean(26, 35, "c4", "p4");

        lst.add(ip1);
        lst.add(ip2);
        lst.add(ip3);
        lst.add(ip4);

        IpInfoBean value = new IpInfoBean(28, 28, null, null);

        int idx = search(lst, value);
        System.out.println(idx);


        /**
         * ip地址串转 long 值
         */
        String ip = "10.10.6.20";
        String[] split = ip.split("\\.");
        long ipvalue = ((long)Integer.parseInt(split[0]) << 24 ) + ((long)Integer.parseInt(split[1]) << 16 ) + ((long)Integer.parseInt(split[2]) << 8 ) +(long)Integer.parseInt(split[3]);


    }


    /*public static int search2(List<IpInfoBean> lst, IpInfoBean bean){

    }*/



}
